﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using viBook.Models;
using viBook.Helpers;
using viBook.Repositories;

namespace viBook.Controllers
{
    public class CategoriesController : BaseController
    {
        #region Constructors and Repository vars

        private readonly IBookRepository _bookRepository;
        private readonly ICategoryRepository _categoryRepository;
        private readonly IContentTagRepository _contenttagRepository;
        private readonly IUserRepository _userRepository;

        public CategoriesController(ICategoryRepository categoryRepository, IContentTagRepository contenttagRepository,
                                    IUserRepository userRepository, IBookRepository bookRepository)
        {
            _categoryRepository = categoryRepository;
            _contenttagRepository = contenttagRepository;
            _userRepository = userRepository;
            _bookRepository = bookRepository;
        }

        #endregion

        public ActionResult Details(int id)
        {
            ViewBag.ListMostPopularTags = _contenttagRepository.GetMostPopularTags();
            ViewBag.ListMostPopularUser = _userRepository.GetMostPopularUsers();
            ViewBag.ListMostPopularBook = _bookRepository.GetMostPopularBook();
            Category category = _categoryRepository.FindCategoryWithAllBooks(id);
            return View(category);
        }


        public string Search(string q, int limit)
        {
            IEnumerable<string> result = _categoryRepository.FindCategories(q, limit);
            return String.Join(Environment.NewLine, result);
        }

        public ActionResult FetchNextBookPage(int categoryId, int pageNumber)
        {
            List<Book> listBooks = _categoryRepository.FetchNextBookPage(categoryId, pageNumber).ToList();
            return Json(listBooks.ListBookToJsonResult());
        }
    }
}